package com.cn.kabalifeuser.ui.my.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.LazyFragment;
import com.cn.kabalifeuser.callback.SchedulerTransformer;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.event.RxBus;
import com.cn.kabalifeuser.event.RxBusBean;
import com.cn.kabalifeuser.ui.integral.IntegralMainActivity;
import com.cn.kabalifeuser.ui.integral.activity.CommoditySearchActivity;
import com.cn.kabalifeuser.ui.integral.activity.IntegralCommodityDetailsActivity;
import com.cn.kabalifeuser.ui.my.bean.CouponTypeListBean;
import com.cn.kabalifeuser.ui.my.bean.MyCouponListBean;
import com.cn.kabalifeuser.ui.service.activity.BusinessHomeActivity;
import com.cn.kabalifeuser.ui.service.activity.MerchantsListActivity;
import com.cn.kabalifeuser.utils.RxToast;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * Created by 晓林 on 2020/9/7.
 * function:
 */
public class CardrollFragments extends LazyFragment<CardrollView, CardrollPresenter> implements CardrollView {
    /**
     * 是否刷新
     * true 说明有下一页
     */
    public boolean isRefresh = true;
    @BindView(R.id.rv_notice)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private View notDataView;
    List<MyCouponListBean.ResultBean.DataBean> mList;
    CardrollAdapter mAdapter;
    private int mId;
    private int toId;

    public static CardrollFragments getInstance(int mid, int toId) {
        CardrollFragments fragment = new CardrollFragments();
        fragment.mId = mid;
        fragment.toId = toId;
        return fragment;
    }

    @Override
    protected CardrollPresenter createPresenter() {
        return new CardrollPresenter();
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.recyclerview_refresh;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ButterKnife.bind(this, view);
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager3 = new LinearLayoutManager(getMe());
        layoutManager3.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager3.setSmoothScrollbarEnabled(true);
        layoutManager3.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(layoutManager3);
        recyclerView.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new CardrollAdapter(R.layout.fragment_cardroll_item, mList);
//                mAdapter = new CouponAdapter(R.layout.pw_commodity_coupon_item, mList, mId);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(mAdapter);
        notDataView = getLayoutInflater().inflate(R.layout.show_empty_view, (ViewGroup) recyclerView.getParent(), false);
        notDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshLayout.autoRefresh();
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MyCouponListBean.ResultBean.DataBean bean = mAdapter.getItem(position);
                //跳转页面：1：积分商城，2：商品详情，3：分类下的商品，4：商家列表，5：商家详情，6：类型下的商家
                if (bean.getPlace() == 1) {
                    $startActivity(IntegralMainActivity.class);
                } else if (bean.getPlace() == 2) {
                    //2：商品详情
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", bean.getPlace_val().getId());
                    $startActivity(IntegralCommodityDetailsActivity.class, bundle);
                } else if (bean.getPlace() == 3) {
                    //2：分类下的商品
                    Bundle bundle = new Bundle();
                    bundle.putInt("sign", 2);
                    bundle.putInt("category_id", bean.getPlace_val().getId());
                    $startActivity(CommoditySearchActivity.class, bundle);
                } else if (bean.getPlace() == 4) {
                    //2：商家列表
                    Bundle bundle = new Bundle();
                    bundle.putInt("sign", 0);
                    bundle.putInt("cityID", LoginHelper.getCityData().getResult().getPid());
                    bundle.putInt("areaID", LoginHelper.getCityData().getResult().getId());
                    $startActivity(MerchantsListActivity.class, bundle);
                } else if (bean.getPlace() == 5) {
                    //2：商家详情
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", bean.getPlace_val().getId());
                    $startActivity(BusinessHomeActivity.class, bundle);
                } else if (bean.getPlace() == 6) {
                    //2：类型下的商家
                    Bundle bundle = new Bundle();
                    bundle.putInt("sign", 1);
                    bundle.putInt("service_type", bean.getPlace_val().getId());
                    bundle.putInt("cityID", LoginHelper.getCityData().getResult().getPid());
                    bundle.putInt("areaID", LoginHelper.getCityData().getResult().getId());
                    $startActivity(MerchantsListActivity.class, bundle);
                }


            }
        });
        initRefreshLayout();
        pagehttp = 1;
        getListData();

        rxBusSubscription = RxBus.getDefault().toObservable(RxBusBean.class)
                .compose(new SchedulerTransformer<RxBusBean>())
                .subscribe(new Action1<RxBusBean>() {
                    @SuppressLint("WrongConstant")
                    @Override
                    public void call(RxBusBean rxBusBean) {
                        if (rxBusBean.getId() == 1 && rxBusBean.getSign() == 1) {
                            mId = (int) rxBusBean.getObjBean();
                            refreshLayout.autoRefresh();
                        }

                    }
                });
    }

    /**
     * 刷新
     */
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    private void getListData() {
        mPresenter.onCouponListData(LoginHelper.getLonginData().getResult(), mId, toId, pagehttp, pageNum);
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        RxToast.error(msg);

    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onCouponTypeSuccess(CouponTypeListBean bean) {

    }

    @Override
    public void onCouponListSuccess(MyCouponListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
                mAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }


    class CardrollAdapter extends BaseQuickAdapter<MyCouponListBean.ResultBean.DataBean, BaseViewHolder> {


        public CardrollAdapter(int layoutResId, @Nullable List<MyCouponListBean.ResultBean.DataBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final MyCouponListBean.ResultBean.DataBean item) {
            helper.setText(R.id.title, item.getTitle())
                    .setText(R.id.subtitle, item.getSubtitle())
                    .setText(R.id.time, item.getTime())
                    .setText(R.id.money, item.getMoney());
        }


    }
}



